package com.example.forhelp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btn_SOS;
    private MyReceiver receiver;
    private MyReceiver2 receiver2;
    private MyReceiver3 receiver3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver(); //注册广播接收者
        init();
    }

    private void registerReceiver() {
        //动态注册1广播
        receiver = new MyReceiver();
        IntentFilter filter1 = new IntentFilter();
        filter1.setPriority(1000); //设置广播的优先级别
        filter1.addAction("Intercept_Stitch");
        registerReceiver(receiver, filter1);
        // 动态注册2广播
        receiver2 = new MyReceiver2();
        IntentFilter filter2 = new IntentFilter();
        filter2.setPriority(200); //设置广播的优先级别
        filter2.addAction("Intercept_Stitch");
        registerReceiver(receiver2, filter2);
        // 动态注册3广播
        receiver3 = new MyReceiver3();
        IntentFilter filter3 = new IntentFilter();
        filter3.setPriority(600); //设置广播的优先级别
        filter3.addAction("Intercept_Stitch");
        registerReceiver(receiver3, filter3);
    }

    private void init(){
        btn_SOS = findViewById(R.id.btn_sendSOS);
        String action = "com.example.forhelp.sos";
        IntentFilter intentFilter = new IntentFilter(action);
        receiver = new MyReceiver();
        registerReceiver(receiver,intentFilter);
        btn_SOS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //String action = "com.example.forhelp.sos";
                Intent intent = new Intent();
                intent.setAction("Intercept_Stitch");
                //sendBroadcast(intent);
                sendOrderedBroadcast(intent,null);
            }
        });
    }

    @Override
    protected  void onDestroy(){
        super.onDestroy();
        unregisterReceiver(receiver);
    }

}